﻿using System.Windows.Controls;
using DataStore.Domain;
using System;
using Infrastructure.ViewModels;

namespace UI.Controls
{
    /// <summary>
    /// Interaction logic for AddAuthor.xaml
    /// </summary>
    public partial class AddAuthor : UserControl
    {
        readonly AuthorViewModel _author;
        public AuthorViewModel Author
        {
            get
            {
                return _author;
            }
        }

        public event EventHandler<EventArgs> Saved;
        protected virtual void OnSaved(object sender, EventArgs e)
        {
            if (Saved != null)
            {
                Saved(sender, e);
            }
        }

        public event EventHandler<EventArgs> AddClicked;
        protected virtual void OnAddClicked(object sender, EventArgs e)
        {
            if (AddClicked != null)
            {
                AddClicked(sender, e);
            }
        }

        public event EventHandler<EventArgs> CancelClicked;
        protected virtual void OnCancelClicked(object sender, EventArgs e)
        {
            if (CancelClicked != null)
            {
                CancelClicked(sender, e);
            }
        }

        public AddAuthor()
        {
            this.InitializeComponent();

            _author = new AuthorViewModel(new Author());
            _author.Updated += new System.EventHandler<System.EventArgs>(delegate
                                                                             {
                                                                                 OnSaved(this, EventArgs.Empty);
                                                                             });
            DataContext = _author;
            addCancel.AddButton.Command = _author.SaveCommand;
        }

        private void addCancel_AddClicked(object sender, System.Windows.RoutedEventArgs e)
        {
            OnAddClicked(this, EventArgs.Empty);
        }

        private void addCancel_CancelClicked(object sender, System.Windows.RoutedEventArgs e)
        {
            OnCancelClicked(this, EventArgs.Empty);
        }
    }
}